#include <stdio.h>

#define MAX_ROW 5
#define MAX_COL 5
#define QUEUE_SIZE 512

struct point { int row, col, predecessor; } queue[QUEUE_SIZE];
int head = 0, tail = 0;

int is_empty(void) {
    return head == tail;
}

int is_full(void) {
    return (tail + 1) % QUEUE_SIZE == head;
}

void enqueue(struct point p) {
    if (!is_full()) {
        queue[tail] = p;
        tail = (tail + 1) % QUEUE_SIZE;
    }
}

struct point dequeue(void) {
    struct point p = queue[head];
    head = (head + 1) % QUEUE_SIZE;
    return p;
}

int maze[MAX_ROW][MAX_COL] = {
    0, 1, 0, 0, 0,
    0, 1, 0, 1, 0,
    0, 0, 0, 0, 0,
    0, 1, 1, 1, 0,
    0, 0, 0, 1, 0,
};

void visit(int row, int col) {
    struct point visit_point = { row, col, (head - 1 + QUEUE_SIZE) % QUEUE_SIZE };
    maze[row][col] = 2;
    enqueue(visit_point);
}

int main(void) {
    struct point p = { 0, 0, -1 };

    maze[p.row][p.col] = 2;
    enqueue(p);
    
    while (!is_empty()) {
        p = dequeue();
		if (p.row == MAX_ROW - 1  /* goal */
		    && p.col == MAX_COL - 1)
			break;
		if (p.col+1 < MAX_COL     /* right */
		    && maze[p.row][p.col+1] == 0)
			visit(p.row, p.col+1);
		if (p.row+1 < MAX_ROW     /* down */
		    && maze[p.row+1][p.col] == 0)
			visit(p.row+1, p.col);
		if (p.col-1 >= 0          /* left */
		    && maze[p.row][p.col-1] == 0)
			visit(p.row, p.col-1);
		if (p.row-1 >= 0          /* up */
		    && maze[p.row-1][p.col] == 0)
			visit(p.row-1, p.col);
    }
    
    if (p.row == MAX_ROW - 1 && p.col == MAX_COL - 1) {
        printf("找到路径：\n");
        printf("(%d, %d)\n", p.row, p.col);
        while (p.predecessor != -1) {
            p = queue[p.predecessor];
            printf("(%d, %d)\n", p.row, p.col);
        }
    } else {
        printf("No path!\n");
    }

    return 0;
}